package com.lkd.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lkd.common.VMSystem;
import com.lkd.entity.VendingMachineEntity;
import com.lkd.service.VendingMachineService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.util.ShardingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class VmsJob {

    @Autowired
    private VendingMachineService vendingMachineService;

    /**
     * 售货机定时补货扫描
     * @param param
     * @return
     */
    @XxlJob("autoSupplyJobHandler")
    public ReturnT<String> autoSupplyJobHandler(String param){
        //分片查询运营状态的售货机
        ShardingUtil.ShardingVO shardingVo = ShardingUtil.getShardingVo();
        int numbers = shardingVo.getTotal();  //分片总数
        int index = shardingVo.getIndex(); //当前分片索引
        QueryWrapper<VendingMachineEntity> qw=new QueryWrapper<>();
        qw.lambda()
                .eq(VendingMachineEntity::getVmStatus, VMSystem.VM_STATUS_RUNNING)
                .apply("mod(id,"+numbers+" ) = "+index);
        List<VendingMachineEntity> machineEntities = vendingMachineService.list(qw);

        for (VendingMachineEntity machineEntity : machineEntities) {
            //调用定时补货业务
            vendingMachineService.supply(machineEntity.getInnerCode());
        }
        return ReturnT.SUCCESS;
    }

}
